package com.gqj.bas.controller;

import com.gqj.bas.domain.BasUnit;
import com.gqj.bas.service.IBasUnitService;
import com.gqj.common.annotation.Log;
import com.gqj.common.constant.UserConstants;
import com.gqj.common.core.controller.BaseController;
import com.gqj.common.core.domain.AjaxResult;
import com.gqj.common.core.page.TableDataInfo;
import com.gqj.common.enums.BusinessType;
import com.gqj.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 计量单位Controller
 * 
 * @author lxq
 * @date 2023-12-17
 */
@RestController
@RequestMapping("/bas/basUnit")
public class BasUnitController extends BaseController
{
    @Autowired
    private IBasUnitService basUnitService;

    /**
     * 查询计量单位列表
     */
    @PreAuthorize("@ss.hasPermi('bas:basUnit:list')")
    @GetMapping("/list")
    public TableDataInfo list(BasUnit basUnit)
    {
        startPage();
        basUnit.setDelFlag("0");
        List<BasUnit> list = basUnitService.selectBasUnitList(basUnit);
        return getDataTable(list);
    }

    /**
     * 查询计量单位列表
     */
    @PreAuthorize("@ss.hasPermi('bas:basUnit:unitOptions')")
    @GetMapping("/unitOptions")
    public AjaxResult unitOptions(BasUnit basUnit)
    {
        List<Map<String, Object>> list = basUnitService.unitOptions(basUnit);
        return AjaxResult.success(list);
    }

    /**
     * 导出计量单位列表
     */
    @PreAuthorize("@ss.hasPermi('bas:basUnit:export')")
    @Log(title = "计量单位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasUnit basUnit)
    {
        List<BasUnit> list = basUnitService.selectBasUnitList(basUnit);
        ExcelUtil<BasUnit> util = new ExcelUtil<BasUnit>(BasUnit.class);
        util.exportExcel(response, list, "计量单位数据");
    }

    /**
     * 获取计量单位详细信息
     */
    @PreAuthorize("@ss.hasPermi('bas:basUnit:query')")
    @GetMapping(value = "/{unitId}")
    public AjaxResult getInfo(@PathVariable("unitId") Long unitId)
    {
        return AjaxResult.success(basUnitService.selectBasUnitByUnitId(unitId));
    }

    /**
     * 新增计量单位
     */
    @PreAuthorize("@ss.hasPermi('bas:basUnit:add')")
    @Log(title = "计量单位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasUnit basUnit)
    {
        if (UserConstants.NOT_UNIQUE.equals(basUnitService.checkUnitNameUnique(basUnit)))
        {
            return AjaxResult.error("新增计量单位'" + basUnit.getUnitName() + "'失败，单位名称已存在");
        }
        basUnit.setCreateBy(getUsername());
        return toAjax(basUnitService.insertBasUnit(basUnit));
    }

    /**
     * 修改计量单位
     */
    @PreAuthorize("@ss.hasPermi('bas:basUnit:edit')")
    @Log(title = "计量单位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasUnit basUnit)
    {
        if (UserConstants.NOT_UNIQUE.equals(basUnitService.checkUnitNameUnique(basUnit)))
        {
            return AjaxResult.error("新增计量单位'" + basUnit.getUnitName() + "'失败，单位名称已存在");
        }
        basUnit.setUpdateBy(getUsername());
        return toAjax(basUnitService.updateBasUnit(basUnit));
    }

    /**
     * 删除计量单位
     */
    @PreAuthorize("@ss.hasPermi('bas:basUnit:remove')")
    @Log(title = "计量单位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{unitIds}")
    public AjaxResult remove(@PathVariable Long[] unitIds)
    {
        for(Long unitId: unitIds){
            if (basUnitService.checkMaterialExistUnit(unitId))
            {
                return AjaxResult.error("计量单位已被引用,不允许删除");
            }
        }
        return toAjax(basUnitService.deleteBasUnitByUnitIds(unitIds));
    }
}
